# encoding = utf-8
class FeedbacksController < ApplicationController

  before_filter :authenticate_admin!, :only => [:list, :delete]
  before_filter :authenticate_user!, :only => [:new, :create]

  def index
    redirect_to :action => :new
  end
  
  def new
    @feedback = Feedback.new
  end

  def create
    params[:feedback][:user_id] = current_user.id
    @feedback = Feedback.new(params[:feedback])

    if @feedback.save
      flash[:success] = '感谢您的留言'
    else
      flash[:error] = @feedback.errors
    end

    redirect_to '/home'
  end

  def list
    @feedbacks = Feedback.order('created_at desc').paginate :page => params[:page], :per_page => PAGE_SIZE
    render :layout => 'admins'
  end

  def delete
    feedback = Feedback.find(params[:id])
    if feedback.destroy
      flash[:success] = '留言删除成功'
    end
    redirect_to :action => "list"
  end
end
